﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModel;
using DataLayer;
using Helper;
namespace Service
{
    public class Admin
    {
        #region register shop
        public static List<Shop> GetShops(string areaCode = "", int groupId = 0)
        {
            List<Shop> shops = new List<Shop>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    shops = (from s in DB.shops
                             join a in DB.areas on s.area_code equals a.code into ar
                             from a in ar.DefaultIfEmpty()
                             join g in DB.groups on s.group_id equals g.id into gr
                             from g in gr.DefaultIfEmpty()
                             where !s.is_deleted
                             select new Shop()
                             {
                                 ID = s.id,
                                 Name = s.name,
                                 Department = s.department,
                                 Area = new Area() { ID = a.id, Name = a.name, Description = a.description, AreaCode = a.code },
                                 Group = new Group() { ID = g.id, Name = g.name, Description = g.description },
                                 UDF1 = s.udf1,
                                 UDF2 = s.udf2,
                                 UDF3 = s.udf3,
                                 UDF4 = s.udf4,
                                 UDF5 = s.udf5
                             })
                             .OrderBy(x => x.Name)
                             .ToList();

                    if (!string.IsNullOrEmpty(areaCode))
                        shops = shops.Where(x => x.Area.AreaCode == areaCode).ToList();
                    if (groupId > 0)
                        shops = shops.Where(x => x.Group.ID == groupId).ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetShops", ex);
                throw;
            }
            
            return shops;
        }

        public static int SaveShop(Shop obj)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    if (obj.ID == 0)
                    {
                        shop s = new shop()
                        {
                            name = obj.Name,
                            department = obj.Department,
                            area_code = obj.Area.AreaCode,
                            group_id = obj.Group.ID,
                            udf1 = obj.UDF1,
                            udf2 = obj.UDF2,
                            udf3 = obj.UDF3,
                            udf4 = obj.UDF4,
                            udf5 = obj.UDF5,
                            date_created = DateTime.Now,
                            is_deleted = false
                        };
                        DB.shops.Add(s);
                    }
                    else
                    {
                        shop s = DB.shops.Where(x => x.id == obj.ID).FirstOrDefault();
                        s.name = obj.Name;
                        s.area_code = obj.Area.AreaCode;
                        s.group_id = obj.Group.ID;
                        s.udf1 = obj.UDF1;
                    }
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("SaveShop", ex);
                throw;
            }
            
            return chk;
        }

        public static int DeleteShop(int id)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    shop s = DB.shops.Where(x => x.id == id).FirstOrDefault();
                    s.is_deleted = true;
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("DeleteShop", ex);
                throw;
            }
           
            return chk;
        }


        #endregion

        #region Area
        public static List<Area> GetAreas()
        {
            List<Area> areas = new List<Area>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    areas = DB.areas.Where(i => !i.is_deleted)
                        .Select(x => new Area()
                        {
                            ID = x.id,
                            Name = x.name,
                            Description = x.description,
                            AreaCode = x.code
                        })
                        .OrderBy(x => x.Name)
                        .ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetAreas", ex);
                throw;
            }
            
            return areas;
        }

        public static int SaveArea(Area obj)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    if (obj.ID > 0)
                    {
                        area a = DB.areas.Where(x => x.id == obj.ID).FirstOrDefault();
                        a.name = obj.Name;
                        a.description = obj.Description;
                    }
                    else
                    {
                        area a = new area()
                        {
                            name = obj.Name,
                            description = obj.Description,
                            date_created = DateTime.Now,
                            code = obj.AreaCode,
                            is_deleted = false
                        };
                        area max = DB.areas.OrderByDescending(x => x.id).FirstOrDefault();
                        int newId = max != null ? max.id + 1 : 1;
                        a.code = Utility.ToAreaCode(newId);
                        DB.areas.Add(a);
                    }
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("SaveArea", ex);
                throw;
            }
            
            return chk;
        }

        public static int DeleteArea(int id)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    area a = DB.areas.Where(x => x.id == id).FirstOrDefault();
                    a.is_deleted = true;
                    chk = DB.SaveChanges();
                }

            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("DeleteArea", ex);
                throw;
            }            
            return chk;
        }


        #endregion

        #region Group
        public static List<Group> GetGroups()
        {
            List<Group> groups = new List<Group>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    groups = DB.groups.Where(i => !i.is_deleted)
                        .Select(x => new Group()
                        {
                            ID = x.id,
                            Name = x.name,
                            Description = x.description
                        })
                        .OrderBy(x => x.Name)
                        .ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetGroups", ex);
                throw;
            }            
            return groups;
        }

        public static int SaveGroup(Group obj)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    if (obj.ID > 0)
                    {
                        group g = DB.groups.Where(x => x.id == obj.ID).FirstOrDefault();
                        g.name = obj.Name;
                        g.description = obj.Description;
                        chk = DB.SaveChanges();
                    }
                    else
                    {
                        group g = new group()
                        {
                            name = obj.Name,
                            description = obj.Description,
                            date_created = DateTime.Now,
                            is_deleted = false
                        };
                        DB.groups.Add(g);
                        chk = DB.SaveChanges();

                    }
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("SaveGroup", ex);
                throw;
            }
            
            return chk;
        }

        public static int DeleteGroup(int id)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    group g = DB.groups.Where(x => x.id == id).FirstOrDefault();
                    g.is_deleted = true;
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("DeleteGroup", ex);
                throw;
            }
            
            return chk;
        }
        #endregion

        #region User

        public static List<DropDownItem> GetRoles()
        {
            List<DropDownItem> items = new List<DropDownItem>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    items = DB.roles.Where(x => !x.is_deleted.Value).Select(o => new DropDownItem()
                    {
                        ID = o.id,
                        Name = o.name
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog("GetRoles", ex);
                throw;
            }
            return items;
        }

        public static List<Member> GetUsers()
        {
            List<Member> users = new List<Member>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    users = (from u in DB.users
                             join s in DB.shops on u.shop_id equals s.id
                             where !u.is_deleted
                             orderby s.name ascending
                             select new Member()
                             {
                                 ID = u.id,
                                 UserName = u.username,
                                 Password = u.password,
                                 Role = (Role)u.role_id,
                                 Shop = new Shop() { ID = s.id, Name = s.name, Department = s.department, UDF1 = s.udf1 },
                                 DateCreated = u.date_created.Value
                             }
                               ).ToList();
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog("GetUsers", ex);
                throw;
            }
            return users;
        }

        public static int SaveUser(Member obj)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    if (obj.ID == 0)
                    {
                        user o = new user()
                        {
                            username = obj.UserName,
                            password = obj.Password,
                            role_id = (int)obj.Role,
                            shop_id = obj.ShopID,
                            date_created = DateTime.Now,
                            is_deleted = false
                        };
                        DB.users.Add(o);
                    }
                    else
                    {
                        user o = DB.users.Where(x => x.id == obj.ID).FirstOrDefault();
                        o.username = obj.UserName;
                        o.password = obj.Password;
                        o.role_id = (int)obj.Role;
                        o.shop_id = obj.ShopID;
                    }
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog("SaveUser", ex);
                throw;
            }
            return chk;
        }

        public static int DeleteUser(int id)
        {
            int chk = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    user o = DB.users.Where(x => x.id == id).FirstOrDefault();
                    o.is_deleted = true;
                    chk = DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog("DeleteUser", ex);
                throw;
            }
            return chk;
        }
        #endregion
    }
}
